package com.senseauto.networkstatus;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.CheckBox;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

public class NetworkUtils {

    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context
                .getApplicationContext().getSystemService(
                        Context.CONNECTIVITY_SERVICE);

        if (manager == null) {
            return false;
        }

        NetworkInfo networkinfo = manager.getActiveNetworkInfo();

        if (networkinfo == null || !networkinfo.isAvailable()) {
            return false;
        }
        return true;
    }

    public static boolean isCarNetworkAvailable(Context context) {
        return isConnected(context, ConnectivityManager.TYPE_WIFI) ||
                isConnected(context, ConnectivityManager.TYPE_MOBILE);
    }

    private static boolean isConnected(Context context, int networkType) {
        ConnectivityManager manager = (ConnectivityManager) context
                .getApplicationContext().getSystemService(
                        Context.CONNECTIVITY_SERVICE);

        if (manager == null) {
            return false;
        }
        NetworkInfo networkInfo = manager.getNetworkInfo(networkType);
        return networkInfo != null && networkInfo.isConnected();
    }

    //下面两个方法是从网上抄的，判断ping 和socket的方式判断网络是否正常的

    public static  boolean isNetworkOnline() {
        Runtime runtime = Runtime.getRuntime();
        try {
            //www.baidu.com 可以代码ping通(代码ping 114.114.114.114不行)
            //Process ipProcess = runtime.exec("ping -c 1 platform-access.gacicv.com -W 5 ");
            //默认超时时间10s, -W设置超时时间多少秒（-w可以针对windows系统但是不适合linux系统）
            Process ipProcess = runtime.exec("ping -c 1 platform-access.gacicv.com  ");
            int exitValue = ipProcess.waitFor();
            return (exitValue == 0);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 通过socket检查外网的连通性
     */
    private static Socket s ;
    public static boolean hasNetworkConnection(Context context){
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        boolean connected = (null != activeNetworkInfo) && activeNetworkInfo.isConnected();
        if(!connected) return false;
        boolean routeExists ;
        try {
            if(s==null){
                s = new Socket();
            }
            //可以换成www.baidu.com，但是我们请求token的是下面的网址
            InetAddress host = InetAddress.getByName("platform-access.gacicv.com");
            s.connect(new InetSocketAddress(host,80),3000);//google:53 //5s 会anr的吧
            routeExists = true;
            s.close();
        } catch (IOException e) {
            routeExists = false ;
        }
        return connected && routeExists ;
    }
}
